/*_________________________
 * MenuDemo.c -- Menu Demostration
 *				(c) Charles Petzold, 1998
 */
#include <Windows.h>

#define IDM_FILE_NEW 		40001
#define IDM_FILE_OPEN 		40002
#define IDM_FILE_SAVE 		40003
#define IDM_FILE_SAVE_AS 	40004
#define IDM_APP_EXIT 		40005
#define IDM_EDIT_UNDO 		40006
#define IDM_EDIT_CUT 		40007
#define IDM_EDIT_COPY 		40008
#define IDM_EDIT_PASTE 		40009
#define IDM_EDIT_CLEAR 		40010
#define IDM_BKGND_WHITE 	40011
#define IDM_BKGND_LTGRAY 	40012
#define IDM_BKGND_GRAY 		40013
#define IDM_BKGND_DKGRAY 	40014
#define IDM_BKGND_BLACK 	40015
#define IDM_TIMER_START 	40016
#define IDM_TIMER_STOP 		40017
#define IDM_APP_HELP 		40018
#define IDM_APP_ABOUT 		40019
#define IDM_APP_DESTROY 	40020

#define ID_TIMER 1

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

TCHAR szAppName[] = TEXT("MenuDemo");

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
	HWND hwnd;
	MSG msg;
	WNDCLASS wndclass;

	wndclass.style = CS_HREDRAW | CS_VREDRAW;
	wndclass.lpfnWndProc = WndProc;
	wndclass.cbClsExtra = 0;
	wndclass.cbWndExtra = 0;
	wndclass.hInstance = hInstance;
	// Load a custom icon from a PNG file
	// Windows does not support loading icons directly from PNG.
	// You need to convert the PNG to an .ico file first.
	// Suppose you have "icon.ico" in your project directory:
	// wndclass.hIcon = (HICON)LoadImage(NULL, TEXT("icon.ico"), IMAGE_ICON, 0, 0, LR_LOADFROMFILE | LR_DEFAULTSIZE);
	wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
	wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wndclass.lpszMenuName = NULL;
	wndclass.lpszClassName = szAppName;

	if (!RegisterClass(&wndclass))
	{
		MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
		return 0;
	}

	hwnd = CreateWindow(szAppName, szAppName,
		// TEXT("Color Scroll"),
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, CW_USEDEFAULT,
		600, 600,
		NULL, NULL, hInstance, NULL);
	ShowWindow(hwnd, iCmdShow);
	UpdateWindow(hwnd);

	while (GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return msg.wParam;
}

void SetStandardMenu(HMENU hMenu)
{
	HMENU hFileMenu = CreatePopupMenu();

	AppendMenu(hFileMenu, MF_STRING, IDM_FILE_NEW, TEXT("&New"));
	AppendMenu(hFileMenu, MF_STRING, IDM_FILE_OPEN, TEXT("&Open..."));
	AppendMenu(hFileMenu, MF_STRING, IDM_FILE_SAVE, TEXT("&Save"));
	AppendMenu(hFileMenu, MF_STRING, IDM_FILE_SAVE_AS, TEXT("Save &As..."));
	AppendMenu(hFileMenu, MF_SEPARATOR, 0, NULL);
	AppendMenu(hFileMenu, MF_STRING, IDM_APP_EXIT, TEXT("E&xit"));
	AppendMenu(hMenu, MF_POPUP, (UINT_PTR)hFileMenu, TEXT("&File"));

	HMENU hEditMenu = CreatePopupMenu();
	AppendMenu(hEditMenu, MF_STRING, IDM_EDIT_UNDO, TEXT("&Undo"));
	AppendMenu(hEditMenu, MF_SEPARATOR, 0, NULL);
	AppendMenu(hEditMenu, MF_STRING, IDM_EDIT_CUT, TEXT("Cu&t"));
	AppendMenu(hEditMenu, MF_STRING, IDM_EDIT_COPY, TEXT("&Copy"));
	AppendMenu(hEditMenu, MF_STRING, IDM_EDIT_PASTE, TEXT("&Paste"));
	AppendMenu(hEditMenu, MF_STRING, IDM_EDIT_CLEAR, TEXT("&Delete"));
	AppendMenu(hMenu, MF_POPUP, (UINT_PTR)hEditMenu, TEXT("&Edit"));

	HMENU hBKGMenu = CreatePopupMenu();
	AppendMenu(hBKGMenu, MF_STRING | MF_CHECKED, IDM_BKGND_WHITE, TEXT("&White"));
	AppendMenu(hBKGMenu, MF_STRING, IDM_BKGND_LTGRAY, TEXT("&Light Gray"));
	AppendMenu(hBKGMenu, MF_STRING, IDM_BKGND_GRAY, TEXT("&Gray"));
	AppendMenu(hBKGMenu, MF_STRING, IDM_BKGND_DKGRAY, TEXT("&Dark Gray"));
	AppendMenu(hBKGMenu, MF_STRING, IDM_BKGND_BLACK, TEXT("&Black"));
	AppendMenu(hMenu, MF_POPUP, (UINT_PTR)hBKGMenu, TEXT("&BackGround"));

	HMENU hTimerMenu = CreatePopupMenu();
	AppendMenu(hTimerMenu, MF_STRING, IDM_TIMER_START, TEXT("&Start"));
	AppendMenu(hTimerMenu, MF_STRING | MF_GRAYED, IDM_TIMER_STOP, TEXT("S&top"));
	AppendMenu(hMenu, MF_POPUP, (UINT_PTR)hTimerMenu, TEXT("&Timer"));

	HMENU hHelpMenu = CreatePopupMenu();
	AppendMenu(hHelpMenu, MF_STRING, IDM_APP_HELP, TEXT("&Help..."));
	AppendMenu(hHelpMenu, MF_STRING, IDM_APP_ABOUT, TEXT("&About MenuDemo..."));
    AppendMenu(hHelpMenu, MF_STRING, IDM_APP_DESTROY, TEXT("&Destroy Menu!"));
	AppendMenu(hMenu, MF_POPUP, (UINT_PTR)hHelpMenu, TEXT("&Help"));
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	static int idColor[5] = { WHITE_BRUSH, LTGRAY_BRUSH, GRAY_BRUSH, DKGRAY_BRUSH, BLACK_BRUSH };
	static int iSelection = IDM_BKGND_WHITE;
	HMENU hMenu;

	switch (message)
	{
	case WM_CREATE:
	{
	    hMenu = CreateMenu();
		SetStandardMenu(hMenu);
		SetMenu(hwnd, hMenu);
		return 0;
	}
	case WM_COMMAND:
	{
		hMenu = GetMenu(hwnd);
		switch (LOWORD(wParam))
		{
		case IDM_FILE_NEW:
		case IDM_FILE_OPEN:
		case IDM_FILE_SAVE:
		case IDM_FILE_SAVE_AS:
			MessageBeep(0);
			return 0;
		case IDM_APP_EXIT:
			SendMessage(hwnd, WM_CLOSE, 0, 0);
			return 0;
		case IDM_EDIT_UNDO:
		case IDM_EDIT_CUT:
		case IDM_EDIT_COPY:
		case IDM_EDIT_PASTE:
		case IDM_EDIT_CLEAR:
			MessageBeep(0);
			return 0;
		case IDM_BKGND_WHITE:	  	// Note: Logic below
		case IDM_BKGND_LTGRAY: 	// assumes that IDM_WHITE
		case IDM_BKGND_GRAY:	  	// through IDM_BLACK are
		case IDM_BKGND_DKGRAY:  	// consecutive numbers in
		case IDM_BKGND_BLACK: { 	// the order shown here.
			CheckMenuItem(hMenu, iSelection, MF_UNCHECKED);
			iSelection = LOWORD(wParam);
			CheckMenuItem(hMenu, iSelection, MF_CHECKED);
			SetClassLong(hwnd, GCLP_HBRBACKGROUND, (LONG)GetStockObject(idColor[LOWORD(wParam) - IDM_BKGND_WHITE]));
			InvalidateRect(hwnd, NULL, TRUE);
			return 0;
		}
		case IDM_TIMER_START: {
			if (SetTimer(hwnd, ID_TIMER, 1000, NULL)) {
				EnableMenuItem(hMenu, IDM_TIMER_START, MF_GRAYED);
				EnableMenuItem(hMenu, IDM_TIMER_STOP, MF_ENABLED);
			}
			return 0;
		}
		case IDM_TIMER_STOP: {
			KillTimer(hwnd, ID_TIMER);
			EnableMenuItem(hMenu, IDM_TIMER_START, MF_ENABLED);
			EnableMenuItem(hMenu, IDM_TIMER_STOP, MF_GRAYED);
			return 0;
		}
		case IDM_APP_HELP: {
			MessageBox(hwnd, TEXT("Help not yet implemented!"), szAppName, MB_ICONEXCLAMATION | MB_OK);
			return 0;
		}
		case IDM_APP_ABOUT: {
			MessageBox(hwnd, TEXT("Menu Demonstartion Program\n") TEXT("(c) Charles Petzold, 1998"), szAppName, MB_ICONINFORMATION | MB_OK);
			return 0;
		}
        case IDM_APP_DESTROY: {
            DestroyMenu(hMenu);
            DrawMenuBar(hwnd);
            return 0;
        }
		}
		return 0;
	}
	case WM_TIMER: {
		MessageBeep(0);
		return 0;
	}
	case WM_DESTROY: {
		PostQuitMessage(0);
		return 0;
	}
	}
	return DefWindowProc(hwnd, message, wParam, lParam);
}